package com.BeatBox;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CustomSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

	private SurfaceHolder holder;
	private Thread thread;
	private int posX = 0;
	private int posY = 0;
	private int width = 50;
	private int height = 50;
	
	public CustomSurfaceView(Context context, SurfaceView sv) {
		super(context);
		// TODO Auto-generated constructor stub
		holder = sv.getHolder();
		holder.addCallback(this);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// SurfaceViewが変化（画面の大きさ，ピクセルフォーマット）した時のイベントの処理を記述
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		/// SurfaceViewが最初に生成されたときに呼び出される
		thread = new Thread( this );
		//loop = true;
		thread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// SurfaceViewが廃棄されたる時の処理を記述
		
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		Canvas canvas = holder.lockCanvas();
		if(canvas != null){
			Paint paint = new Paint();
			paint.setColor(Color.GREEN);
			canvas.drawRect(posX, posY, width, height, paint);
		}
		holder.unlockCanvasAndPost(canvas);
	}


	public void setSizeSurfaceView(int x, int y, int w, int h) {
		// TODO Auto-generated method stub
		posX = x;
		posY = y;
		width = w;
		height = h;
	}

}
